package com.neteast.androidclient.newscenter.domain;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class BootStrapError {
	/**通信协议*/
	public int protocolVersion;
	/**报文id*/
	public int msgId;
	/**错误编号*/
	public int errorId;
	/** 错误描述信息*/
	public String errorDesc;
	
	/**保留字段长度*/
	public int reserveLen;
	/**保留数据*/
	public byte[] reserve;
	
	private ByteBuffer byteBuffer;
	
	public BootStrapError(ByteBuffer byteBuffer) {
		this.byteBuffer=byteBuffer;
		init();
	}

	private void init() {
		protocolVersion=byteBuffer.getInt();
		msgId=byteBuffer.getInt();
		errorId=byteBuffer.getInt();
		//当errorId对应的错误描述信息没有找到时，才执行下一步。
		byte[] error=new byte[20];
		for(int i=0;i<20;i++){
			error[i]=byteBuffer.get();
		}
		try {
			errorDesc=new String(error, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		reserveLen=byteBuffer.getInt();
		if (reserveLen>0) {
			reserve=new byte[reserveLen];
			for (int i = 0; i < reserveLen; i++) {
				reserve[i]=byteBuffer.get();
			}
		}
	}

	@Override
	public String toString() {
		if (reserve!=null) {
			return "BootStrapError [protocolVersion=" + protocolVersion
					+ ", msgId=" + msgId + ", errorId=" + errorId + ", errorDesc="
					+ errorDesc + ", reserveLen=" + reserveLen + ", reserve="
					+ Arrays.toString(reserve) + "]";
		}else {
			return "BootStrapError [protocolVersion=" + protocolVersion
					+ ", msgId=" + msgId + ", errorId=" + errorId + ", errorDesc="
					+ errorDesc + ", reserveLen=" + reserveLen + "]";
		}
	}
	
}
